/**  
 * 
 */
package com.astrocsr.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.astrocsr.domain.bean.TblPermission;

@Repository("accessRightDAO")
public class PermissionDAO extends BaseHelperDAO{

	@SuppressWarnings("unchecked")
	public List<TblPermission> listPermission(String groupCode) throws Exception {
		String strQuery = " select new TblPermission(pe.moduleCode, pe.groupCode, md.moduleName) "
						+ " from TblModule md, TblPermission pe where md.moduleCode=pe.moduleCode and pe.groupCode=:groupCode order by md.moduleCode";
		List<TblPermission> result = null;  
		Query query = getCurrentSession().createQuery(strQuery).setParameter("groupCode", groupCode); 
		result = query.list();

		return result;
	}

	public void addPermission(TblPermission entity) {
		// Insert to database
		getCurrentSession().save(entity);
	}
	
	public void deletePermission(String moduleCode, String groupCode){
		// Delete to database 
		String strQuery = "DELETE FROM TblPermission WHERE moduleCode =:moduleCode and groupCode = :groupCode";
		Query query = getCurrentSession().createQuery(strQuery);
		query.setParameter("moduleCode", moduleCode);
		query.setParameter("groupCode", groupCode);
		query.executeUpdate();
	} 
}
